/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package com.yasee.chronic.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yasee.chronic.R;
import com.yasee.chronic.model.WorkBean;
import com.yasee.chronic.util.MyUtils;

import zuo.biao.library.base.BaseView;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;

/**
 * 用户View
 *
 * @author Lemon
 * @use <br> UserView userView = new UserView(context, resources);
 * <br> adapter中使用:[具体参考.BaseViewAdapter(getView使用自定义View的写法)]
 * <br> convertView = userView.createView(inflater);
 * <br> userView.bindView(position, data);
 * <br> 或  其它类中使用:
 * <br> containerView.addView(userView.createView(inflater));
 * <br> userView.bindView(data);
 * <br> 然后
 * <br> userView.setOnDataChangedListener(onDataChangedListener);data = userView.getData();//非必需
 * <br> userView.setOnClickListener(onClickListener);//非必需
 */
public class UserDetailView extends BaseView<WorkBean.Data> implements OnClickListener {
    private static final String TAG = "UserView";

    public RelativeLayout llTile;
    public LinearLayout llContent;
    private TextView tvBedNo;
    private TextView tvName;
    private TextView tvValue;
    private TextView tvTips;
    private TextView tvTime;
    private TextView tvPlan;

    public UserDetailView(Activity context, ViewGroup parent) {
        super(context, R.layout.item_work_bench_detail, parent);
    }

    private String type = "";

    public void setType(String type) {
        this.type = type;
    }

    @SuppressLint("InflateParams")
    @Override
    public View createView() {
        llTile = findView(R.id.item_title);
        llContent = findView(R.id.item_content);
        tvBedNo = findView(R.id.item_bed_no);
        tvName = findView(R.id.item_name);
        tvValue = findView(R.id.tv_value);
        tvTips = findView(R.id.tv_tip);
        tvTime = findView(R.id.tv_time);
        tvPlan = findView(R.id.tv_plan);
        return super.createView();
    }

    @Override
    public void bindView(WorkBean.Data data_) {
        super.bindView(data_ != null ? data_ : new WorkBean.Data());
        Log.e(TAG,"xxxxxxxxxxxx" + new Gson().toJson(data));
        tvBedNo.setText(StringUtil.getTrimedString(data.getBedName()));
        tvName.setText(StringUtil.getTrimedString(data.getName()));

        switch (type) {
            case "bg":// 血糖
                if(data.getBloodGlucoseLatest() == null){
                    tvValue.setText("-");
                    tvTips.setVisibility(View.INVISIBLE);
                }else{
                    tvValue.setText(data.getBloodGlucoseLatest().getResult()+"");
                    if(data.getBloodGlucoseLatest().getAlarmType() == 0){
                        tvValue.setTextColor(getColor(R.color.green_btn));
                        tvTips.setVisibility(View.INVISIBLE);
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_blue));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_blue));
                    }else if(data.getBloodGlucoseLatest().getAlarmType() == 1){
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↓");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }else{
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↑");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }
                    if(data.getBloodGlucoseLatest().getTestTime()==null){
                        tvTime.setText("");
                    }else{
                        tvTime.setText(MyUtils.getDateTime(Long.parseLong(data.getBloodGlucoseLatest().getTestTime())));
                    }

                    tvPlan.setText("已检/计划：" + data.getBloodGlucoseLatest().getTestCount() +"次/"+ data.getBloodGlucoseLatest().getPlanCount() +"次");
                }
                break;
            case "bp":// 血压
                if(data.getBloodPressureLatest() == null){
                    tvValue.setText("-");
                    tvTips.setVisibility(View.INVISIBLE);
                }else{
                    tvValue.setText(StringUtil.getTrimedString(data.getBloodPressureLatest().getDiastolic()) + "/" + StringUtil.getTrimedString(data.getBloodPressureLatest().getSystolic()));
                    if(data.getBloodPressureLatest().getLevel() == 0 || data.getBloodPressureLatest().getLevel() == 1){
                        tvValue.setTextColor(getColor(R.color.green_btn));
                        tvTips.setVisibility(View.INVISIBLE);
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_blue));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_blue));
                    }else if(data.getBloodPressureLatest().getLevel() > 1){
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↑");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }else{
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↓");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }
                    Log.e(TAG,"格式化：：：：：：：："+data.getBloodPressureLatest().getTestTime());
                    if(data.getBloodPressureLatest().getTestTime()==null){
                        tvPlan.setText("");
                    }else{
                        tvPlan.setText(MyUtils.getDateTime(Long.parseLong(data.getBloodPressureLatest().getTestTime())));
                    }
                    Integer heartRate = data.getBloodPressureLatest().getHeartRate();
                    if(heartRate == null){
                        tvTime.setText("-");
                    }else{
                        tvTime.setText(data.getBloodPressureLatest().getHeartRate()+ "");
                    }
                }
                break;
            case "ua":// 尿酸
                if(data.getUricAcidLatest() == null){
                    tvValue.setText("-");
                    tvTips.setVisibility(View.INVISIBLE);
                }else{
                    if(data.getUricAcidLatest().getAlarmType() == 0){
                        tvValue.setTextColor(getColor(R.color.green_btn));
                        tvTips.setVisibility(View.INVISIBLE);
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_blue));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_blue));
                    }else if(data.getUricAcidLatest().getAlarmType() == 1){
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↓");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }else{
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↑");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }
                    tvValue.setText(data.getUricAcidLatest().getResult()==null? "-" : data.getUricAcidLatest().getResult()+"");
                    if(data.getUricAcidLatest().getTestTime()==null){
                        tvPlan.setText("");
                    }else{
                        tvPlan.setText(MyUtils.getDateTime(Long.parseLong(data.getUricAcidLatest().getTestTime()+ "")));
                    }
                }
                break;
            case "bk":// 血酮
                if(data.getKetoneLatest() == null){
                    tvValue.setText("-");
                    tvTips.setVisibility(View.INVISIBLE);
                }else{
                    if(data.getKetoneLatest().getAlarmType() == 0){
                        tvValue.setTextColor(getColor(R.color.green_btn));
                        tvTips.setVisibility(View.INVISIBLE);
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_blue));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_blue));
                    }else if(data.getKetoneLatest().getAlarmType() == 1){
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↓");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }else{
                        tvValue.setTextColor(getColor(R.color.red));
                        tvTips.setVisibility(View.VISIBLE);
                        tvTips.setText("↑");
                        llTile.setBackground(getResources().getDrawable(R.drawable.rounded_corners_top_red));
                        llContent.setBackground(getResources().getDrawable(R.drawable.rounded_corners_bottom_red));
                    }
                    tvValue.setText(data.getKetoneLatest().getResult()==null? "-" : data.getKetoneLatest().getResult()+"");
                    if(data.getKetoneLatest().getTestTime()==null){
                        tvPlan.setText("");
                    }else {
                        tvPlan.setText(MyUtils.getDateTime(Long.parseLong(data.getKetoneLatest().getTestTime()+ "")));
                    }
                }
                break;
        }

        super.bindView(data_ != null ? data_ : new WorkBean.Data());
    }

    @Override
    public void onClick(View v) {
//        if (BaseModel.isCorrect(data) == false) {
//            return;
//        }
        switch (v.getId()) {
            case R.id.ivUserViewHead:
//                toActivity(WebViewActivity.createIntent(context, data.getName(), data.getHead()));
                break;
            default:
                switch (v.getId()) {
                    case R.id.ivUserViewStar:
//                        data.setStarred(!data.getStarred());
                        break;
                    case R.id.tvUserViewSex:
//                        data.setSex(data.getSex() == User.SEX_FEMALE ? User.SEX_MAIL : User.SEX_FEMALE);
                        break;
                }
                bindView(data);
                break;
        }
    }
}